﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookingHotel.ClasseDiDominio;


namespace BookingHotel.Collezioni
{
    class PeriodsCollection
    {
        public List<Period> PeriodCollection { get; set; } 
        
        public PeriodsCollection()
        {
            PeriodCollection = new List<Period>();
        }

        public void AddPeriod(Period newPeriod)
        {
            if (PeriodCollection.Find(period => period.Id == newPeriod.Id) != null)
                throw new Exception("Duplicate id for period.");
            if (PeriodCollection.Find(period => newPeriod.StartPeriod >= period.StartPeriod && newPeriod.StartPeriod <= period.EndPeriod || newPeriod.EndPeriod >= period.StartPeriod && newPeriod.EndPeriod <= period.EndPeriod) != null)
                throw new Exception("Duplicate id for period.");
            PeriodCollection.Add(newPeriod);
        }

        public void AddPeriod(DateTime startDate, DateTime endDate, decimal coefficent)
        {
            int idOfNewPeriod = PeriodCollection.Count;
            Period newPeriod = new Period(startDate, endDate, coefficent, idOfNewPeriod);
            AddPeriod(newPeriod);
        }

        public List<Period> GetPeriodsByDate(DateTime startDate, DateTime endDate)
        {
            List<Period> allPeriodsInADate = PeriodCollection.ToList().FindAll(period => startDate >= period.StartPeriod && startDate <= period.EndPeriod
                                     || endDate >= period.StartPeriod && endDate <= period.EndPeriod
                                     || startDate <= period.StartPeriod && endDate <= period.EndPeriod);
            return allPeriodsInADate;
        }
        
        public String ToString()
        {
            String allPeriodsToString = "";
            foreach (Period currentPeriod in PeriodCollection)
                allPeriodsToString += currentPeriod.ToString();
            return allPeriodsToString;
        }
    
    }
}
